package com.ruoyi.project.good.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.good.domain.GoodSpec;
import com.ruoyi.project.good.domain.GoodsPhoto;
import com.ruoyi.project.good.mapper.GoodSpecMapper;
import com.ruoyi.project.good.mapper.GoodsPhotoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.good.mapper.GoodsMapper;
import com.ruoyi.project.good.domain.Goods;
import com.ruoyi.project.good.service.IGoodsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-07-01
 */
@Service
public class GoodsServiceImpl implements IGoodsService 
{
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsPhotoMapper goodsPhotoMapper;

    @Autowired
    private GoodSpecMapper goodSpecMapper;

    /**
     * 查询商品
     * 
     * @param goodsId 商品ID
     * @return 商品
     */
    @Override
    public Goods selectGoodsById(Long goodsId)
    {
        Goods g = goodsMapper.selectGoodsById(goodsId);
        g.setSpecList(goodSpecMapper.selectGoodSpecListByGoodId(g.getGoodsId()));
        g.setPhotoList(goodsPhotoMapper.selectGoodsPhotosByGoodId(g.getGoodsId()));
        return g;
    }

    /**
     * 查询商品
     *
     * @param goodsTypeId 商品类型ID
     * @return 商品
     */
    @Override
    public List<Goods> selectGoodsByTypeId(Long goodsTypeId)
    {
        return goodsMapper.selectGoodsByTypeId(goodsTypeId);
    }

    /**
     * 查询商品列表
     * 
     * @param goods 商品
     * @return 商品
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods)
    {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    @Transactional
    public int insertGoods(Goods goods)
    {
        //先保存货物信息
        goods.setCreateTime(DateUtils.getNowDate());
        int goodId = goodsMapper.insertGoods(goods);
        //保存图片信息
        for(GoodsPhoto goodsPhoto : goods.getPhotoList()){
            goodsPhoto.setGoodsId(Long.valueOf(goodId));
            goodsPhotoMapper.insertGoodsPhoto(goodsPhoto);
        }
        //保存规格
        int goodSpecKey = 0;
        //先找出类型信息
        List<GoodSpec> list = new ArrayList<GoodSpec>();
        for (int i=0 ;i<goods.getSpecList().size();i++){
            GoodSpec  goodSpecs = goods.getSpecList().get(i);
            if(goodSpecs.getSpecName() != null){
                list.add(goodSpecs);
                goods.getSpecList().remove(i--);
            }
        }
        //保存类型下的规格信息
        for(GoodSpec s : list){
            s.setGoodsId(Long.valueOf(goodId));
            goodSpecKey = goodSpecMapper.insertGoodSpec(s);
            for(GoodSpec spec : goods.getSpecList()){
                if(spec.getTypeNum() == s.getTypeNum()){
                    spec.setGoodsId(Long.valueOf(goodId));
                    spec.setParentId(Long.valueOf(goodSpecKey));
                    goodSpecMapper.insertGoodSpec(spec);
                }
            }
        }
        return goodId;
    }

    /**
     * 修改商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Override
    @Transactional
    public int updateGoods(Goods goods)
    {
        //修改商品
        int goodId = goodsMapper.updateGoods(goods);
        //修改商品图片
        //删除以前绑定的图片信息
        goodsPhotoMapper.deleteGoodsPhotoByGoodsId(goods.getGoodsId());
        for(GoodsPhoto goodsPhoto : goods.getPhotoList()){
            goodsPhoto.setGoodsId(goods.getGoodsId());
            goodsPhotoMapper.insertGoodsPhoto(goodsPhoto);
        }
        //修改商品规格
        //删除以前绑定的规格
        goodSpecMapper.deleteGoodSpecByGoodsId(goods.getGoodsId());
        //保存规格
        int goodSpecKey = 0;
        //先找出类型信息
        List<GoodSpec> list = new ArrayList<GoodSpec>();
        for (int i=0 ;i<goods.getSpecList().size();i++){
            GoodSpec  goodSpecs = goods.getSpecList().get(i);
            if(goodSpecs.getSpecName() != null){
                list.add(goodSpecs);
                goods.getSpecList().remove(i--);
            }
        }
        //保存类型下的规格信息
        for(GoodSpec s : list){
            s.setGoodsId(goods.getGoodsId());
            goodSpecKey = goodSpecMapper.insertGoodSpec(s);
            for(GoodSpec spec : goods.getSpecList()){
                if(spec.getTypeNum() == s.getTypeNum()){
                    spec.setParentId(Long.valueOf(goodSpecKey));
                    spec.setGoodsId(goods.getGoodsId());
                    goodSpecMapper.insertGoodSpec(spec);
                }
            }
        }
        return goodId;
    }

    /**
     * 批量删除商品
     * 
     * @param goodsIds 需要删除的商品ID
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] goodsIds)
    {
        return goodsMapper.deleteGoodsByIds(goodsIds);
    }

    /**
     * 删除商品信息
     * 
     * @param goodsId 商品ID
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long goodsId)
    {
        goodsPhotoMapper.deleteGoodsPhotoByGoodsId(goodsId);
        goodSpecMapper.deleteGoodSpecByGoodsId(goodsId);
        return goodsMapper.deleteGoodsById(goodsId);
    }
}
